package edu.friday.model.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.friday.model.SysRole;
import edu.friday.model.SysUserRole;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.java.Log;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysUserVO implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long userId;

    @NotBlank(message = "用户账户不能为空")
    @Size(min = 0,max=30,message = "用户账户不能超过30个字符")
    private String userName;

    @Size(min = 0,max = 30,message ="用户昵称不能超过30个字符")
    private String nickName;

    @Email(message = "邮箱格式不正确")
    @Size(min = 0,max=50,message = "邮箱长度不能超过50个字符")
    private String email;

    @Size(min = 0,max = 11,message = "手机号不能超过11位")
    private String phoneNumber;

    private String sex;

    private String userType;

    private String avatar;

    @JsonProperty
    private String password;

    private String salt;

    private String status;

    private  String delFlag;

    private String loginIp;

    private Date loginDate;

    private String createBy;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    private String updateBy;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    private String remark;

    private List<SysRoleVO> roles;

    private Long[] roleIds;

    public SysUserVO(Long userId){
        this.userId=userId;
    }

    public Boolean isAdmin(){
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId){
        return userId !=null && 1L==userId;
    }

//    @ManyToMany(cascade = CascadeType.PERSIST)
//    @JoinTable(name = "sys_user_role",
//            joinColumns = {@JoinColumn(name = "user_id")},
//            inverseJoinColumns = {@JoinColumn(name = "role_id")})
//    List<SysRoleVO> sysRoleVOS;

    @OneToMany(cascade = CascadeType.PERSIST)
            @JoinColumn(name = "user_id")
    List<SysUserRole> sysUserRoles;
}
